//
//  AppDelegate.swift
//  8.31-WH微博
//
//  Created by 吴涵 on 15/8/31.
//  Copyright © 2015年 wuhan. All rights reserved.
//

import UIKit
import AFNetworking
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
/// 统一设置 导航栏的样式
        barAppreace()
        //调用菊花设置
        setupNetwork()
        printLog(UserAccountViewModel.sharedUserAccount.userLogin)
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.orangeColor()
        
        window?.rootViewController = defaultRootViewController()
        
        window? .makeKeyAndVisible()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changController:", name:WHSwithRootControllerNotification, object: nil)
        
        return true
    }
    
    deinit{
    
        NSNotificationCenter.defaultCenter().removeObserver(self, name: WHSwithRootControllerNotification, object: nil)
    }
    func changController(notification: NSNotification) {
        printLog(notification)
        window?.rootViewController = (notification.object == nil) ? WHTabBarController(): WHWelcomeViewController()
    }
    
    private func defaultRootViewController() -> UIViewController{
    
        if UserAccountViewModel.sharedUserAccount.userLogin {
            
        return isNewVersion() ? WHNewFeatureViewController() : WHWelcomeViewController()
        }
        return WHTabBarController()
    }
    private func isNewVersion() -> Bool {
        let bundleVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        let versionKey = "wuhanshidage"
        let saxboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(versionKey)
        NSUserDefaults.standardUserDefaults().setDouble(bundleVersion, forKey: versionKey)
        return bundleVersion > saxboxVersion
    }
    private func setupNetwork(){
    //设置网络指示器，顶上的菊花
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        //设置缓存的大小
        let cache = NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil)
        NSURLCache.setSharedURLCache(cache)
    }

    private func barAppreace(){
    
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
    }
    
}

